package com.whfc.openapi.api.energy.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.enums.AppWarnModuleType;
import com.whfc.common.enums.DeviceType;
import com.whfc.common.exception.BizException;
import com.whfc.common.result.ResultEnum;
import com.whfc.common.util.JSONUtil;
import com.whfc.env.dto.EnvDustWarnGenerateDTO;
import com.whfc.env.entity.EnergyDevice;
import com.whfc.env.entity.EnergyElectricityData;
import com.whfc.env.entity.EnergyWaterData;
import com.whfc.env.service.EnergyDeviceService;
import com.whfc.env.service.EnergyElectricityDataService;
import com.whfc.env.service.EnergyWaterDataService;
import com.whfc.env.service.EnvWarnDataService;
import com.whfc.openapi.api.energy.service.OpenEnergyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class OpenEnergyServiceImpl implements OpenEnergyService {

    @DubboReference(interfaceClass = EnergyElectricityDataService.class, version = "1.0.0")
    private EnergyElectricityDataService energyElectricityDataService;

    @DubboReference(interfaceClass = EnergyWaterDataService.class, version = "1.0.0")
    private EnergyWaterDataService energyWaterDataService;

    @DubboReference(interfaceClass = EnergyDeviceService.class, version = "1.0.0")
    private EnergyDeviceService energyDeviceService;

    @DubboReference(interfaceClass = EnvWarnDataService.class, version = "1.0.0")
    private EnvWarnDataService envWarnDataService;

    @Override
    public void receiveSwznData(String body) throws BizException{
        JSONObject jsonBody = JSONObject.parseObject(body);
        // 设备唯一标识
        String devId = jsonBody.getString("devId");
        // 设备类型  a1MESrVkpAR 水表  yunjia_elec_monitoring电表
        String devType = jsonBody.getString("devType");
        // 事件类型   realTime(用水检测实时数据)  report(运行状态和实时电参量上报)  alarm(告警上报)
        String eventType = jsonBody.getString("eventType");
        // 水表类型 用水检测实时数据 realTime(用水检测实时数据)
        if (("a1MESrVkpAR".equals(devType))&&"realTime".equals(eventType)) {
            EnergyDevice energyDevice = energyDeviceService.selectBySnAndPlatformAndType(devId, "swzn", DeviceType.waterMeter.getValue());
            if (energyDevice == null ) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "水表设备不存在");
            }
            JSONObject params = jsonBody.getJSONObject("msg").getJSONObject("params");
            EnergyWaterData energyWaterData = JSONUtil.parseObject(JSON.toJSONString(params), EnergyWaterData.class);
            energyWaterData.setId(null);
            energyWaterData.setDeviceId(energyDevice.getId());
            energyWaterData.setCreateTime(new Date(params.getLong("time")));
            energyWaterData.setTransientFlow(energyWaterData.getTransientFlow().multiply(new BigDecimal("0.0001")));
            energyWaterData.setBatteryVol(energyWaterData.getBatteryVol().multiply(new BigDecimal("0.01")));
            energyWaterDataService.add(energyWaterData);
        }
        // 电表类型 report(运行状态和实时电参量上报)
        if ("yunjia_elec_monitoring".equals(devType)&&"report".equals(eventType)) {
            EnergyDevice energyDevice = energyDeviceService.selectBySnAndPlatformAndType(devId, "swzn", DeviceType.electricMeter.getValue());
            if (energyDevice == null ) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "电表设备不存在");
            }
            JSONObject params = jsonBody.getJSONObject("msg").getJSONObject("params");
            EnergyElectricityData energyElectricityData = JSONUtil.parseObject(JSON.toJSONString(params), EnergyElectricityData.class);
            energyElectricityData.setId(null);
            energyElectricityData.setDeviceId(energyDevice.getId());
            energyElectricityData.setCreateTime(new Date(params.getLong("time")));
            // 电压(0.1v)
            energyElectricityData.setAPhaseVoltage(energyElectricityData.getAPhaseVoltage().multiply(new BigDecimal("0.1")));
            energyElectricityData.setBPhaseVoltage(energyElectricityData.getBPhaseVoltage().multiply(new BigDecimal("0.1")));
            energyElectricityData.setCPhaseVoltage(energyElectricityData.getCPhaseVoltage().multiply(new BigDecimal("0.1")));
            // 频率(0.01Hz)
            energyElectricityData.setAPhaseFrequence(energyElectricityData.getAPhaseFrequence().multiply(new BigDecimal("0.01")));
            energyElectricityData.setBPhaseFrequence(energyElectricityData.getBPhaseFrequence().multiply(new BigDecimal("0.01")));
            energyElectricityData.setCPhaseFrequence(energyElectricityData.getCPhaseFrequence().multiply(new BigDecimal("0.01")));
            // 电流(0.01A)
            energyElectricityData.setAPhaseCurrent(energyElectricityData.getAPhaseCurrent().multiply(new BigDecimal("0.01")));
            energyElectricityData.setBPhaseCurrent(energyElectricityData.getBPhaseCurrent().multiply(new BigDecimal("0.01")));
            energyElectricityData.setCPhaseCurrent(energyElectricityData.getCPhaseCurrent().multiply(new BigDecimal("0.01")));
            // 漏电流(0.1mA)
            energyElectricityData.setLeakCurrent(energyElectricityData.getLeakCurrent().multiply(new BigDecimal("0.1")));
            // 温度(0.1℃)
            energyElectricityData.setAPhaseTemperature(energyElectricityData.getAPhaseTemperature().multiply(new BigDecimal("0.1")));
            energyElectricityData.setBPhaseTemperature(energyElectricityData.getBPhaseTemperature().multiply(new BigDecimal("0.1")));
            energyElectricityData.setCPhaseTemperature(energyElectricityData.getCPhaseTemperature().multiply(new BigDecimal("0.1")));
            energyElectricityData.setNPhaseTemperature(energyElectricityData.getNPhaseTemperature().multiply(new BigDecimal("0.1")));
            // 电能(0.01KWH)
            energyElectricityData.setAPhaseElectricalEnergy(energyElectricityData.getAPhaseElectricalEnergy().multiply(new BigDecimal("0.01")));
            energyElectricityData.setBPhaseElectricalEnergy(energyElectricityData.getBPhaseElectricalEnergy().multiply(new BigDecimal("0.01")));
            energyElectricityData.setCPhaseElectricalEnergy(energyElectricityData.getCPhaseElectricalEnergy().multiply(new BigDecimal("0.01")));
            energyElectricityDataService.add(energyElectricityData);
        }
        // 电表类型 alarm(告警上报)
        if("yunjia_elec_monitoring".equals(devType)&&"alarm".equals(eventType)){
            JSONObject params = jsonBody.getJSONObject("msg").getJSONObject("params");
            EnergyDevice energyDevice = energyDeviceService.selectBySnAndPlatformAndType(devId, "swzn", DeviceType.electricMeter.getValue());
            if (energyDevice == null ) {
                throw new BizException(ResultEnum.PARAM_ERROR.getCode(), "电表设备不存在");
            }

            EnvDustWarnGenerateDTO envDustWarnGenerateDTO = new EnvDustWarnGenerateDTO();
            envDustWarnGenerateDTO.setDeptId(energyDevice.getDeptId());
            envDustWarnGenerateDTO.setDeviceType(DeviceType.electricMeter.getValue());
            envDustWarnGenerateDTO.setRuleType(params.getInteger("type"));
            envDustWarnGenerateDTO.setTriggerObjectId(String.valueOf(energyDevice.getId()));
            envDustWarnGenerateDTO.setTriggerTime(new Date(params.getLong("time")));
            envDustWarnGenerateDTO.setTriggerValue(String.valueOf(params.getDouble("actionVa")));
            envDustWarnGenerateDTO.setModuleType(AppWarnModuleType.ENERGY.value());
            envWarnDataService.addWarn(envDustWarnGenerateDTO);
        }
    }
}
